/**     
* @Title: TrashServiceImpl.java
* @Package me.springboot.mybatis.service.impl 
* @Description: TODO
* @author zengh 
* @date 2018/04/16
* @version V1.0     
*/
package me.springboot.mybatis.service.impl;

import me.springboot.mybatis.common.utils.StringUtil;
import me.springboot.mybatis.dao.CoverMapper;
import me.springboot.mybatis.dao.TrashMapper;
import me.springboot.mybatis.enmu.DeviceTypeEnum;
import me.springboot.mybatis.model.Trash;
import me.springboot.mybatis.service.TrashService;
import me.springboot.mybatis.core.AbstractService;
import me.springboot.mybatis.web.req.DeviceReq;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**   
* @ClassName: TrashService   
* @Description: TODO
* @author zengh
* @date 2018/04/16
*/
@Service
@Transactional
public class TrashServiceImpl extends AbstractService<Trash> implements TrashService {

    @Resource
    private TrashMapper trashMapper;

    @Resource
    private CoverMapper coverMapper;

    @Override
    public List<Map<String, Object>> search(DeviceReq deviceReq) {
        if(StringUtil.isNotBlank(deviceReq.getType())) {
            return trashMapper.selectAllDevice(deviceReq);
        }else {
            // 井盖
            if(deviceReq.getType().equals("1")) {
                return trashMapper.searchCover(deviceReq);
            }else {//垃圾箱
                return trashMapper.searchTrash(deviceReq);
            }
        }
    }

    @Override
    public List<Map<String, Object>> getDetail(String device_type, String device_num,
                                               String startTime, String endTime) {
        Map<String, Object> param = new HashMap<>();
        param.put("device_num", device_num);
        param.put("device_type", device_type);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        if(device_type.equals("1")) {
            return coverMapper.selectByMap(param);
        }else {
            return trashMapper.selectByMap(param);
        }
    }

    @Override
    public List<Map<String, Object>> countDataYiChang(String device_type, String device_num) {
        Map<String, String> param = new HashMap<>();
        param.put("device_num", device_num);
        if(device_type.equals("1")) {
            return coverMapper.countDataYiChang(param);
        }else {
            return trashMapper.countDataYiChang(param);
        }
    }

    @Override
    public List<Map<String, Object>> getPosition(String device_type, Map<String, String> param) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        if(null != device_type && device_type.equals("1")) {
            resultList =  coverMapper.getPosition(param);
        }else if(null != device_type && device_type.equals("2")){
            resultList = trashMapper.getPosition(param);
        } else {
            List<Map<String, Object>> list1 = coverMapper.getPosition(param);
            List<Map<String, Object>> list2 = trashMapper.getPosition(param);
            resultList.addAll(list1);
            resultList.addAll(list2);
        }
        for(Map<String, Object> map : resultList) {
            if(map.get("real_devicetype").toString().equals(DeviceTypeEnum.JingGai.getCode())) {
                if(map.get("real_device_status").toString().equals("1")) {
                    map.put("imgPath", "jg1.png");
                }else if(map.get("real_device_status").toString().equals("2")) {
                    map.put("imgPath", "jg2.png");
                }else {
                    map.put("imgPath", "jg3.png");
                }
            }else {
                if(map.get("real_device_status").toString().equals("1")) {
                    map.put("imgPath", "lj1.png");
                }else if(map.get("real_device_status").toString().equals("2")) {
                    map.put("imgPath", "lj2.png");
                }else {
                    map.put("imgPath", "lj3.png");
                }
            }
        }
        return resultList;
    }

    @Override
    public Map<String, Object> getSingleDetail(String device_type, String device_num) {
        Map<String, String> param = new HashMap<>();
        param.put("device_num", device_num);
        if(device_type.equals("1")) {
            return coverMapper.getSingleDetail(param);
        }else {
            return trashMapper.getSingleDetail(param);
        }
    }


}